package fileopera

import (
	"fmt"
	"hft/Database/admin/file"
	"hft/global"
)

// 创建这两个库
func CreateDirSystem(username string) {
	global.OC_DB.Table(CreateDirOrFileTable(username, "dir_")).AutoMigrate(&file.Dir{})
	global.OC_DB.Table(CreateDirOrFileTable(username, "file_")).AutoMigrate(&file.File{})
	// 生成外键
	global.OC_DB.Exec(fmt.Sprintf("ALTER TABLE %v ADD CONSTRAINT fk_%v_dir_pid_id FOREIGN KEY (p_id) REFERENCES %v(id) ON DELETE CASCADE;", CreateDirOrFileTable(username, "dir_"), username, CreateDirOrFileTable(username, "dir_")))
	// 生成文件link 索引
	global.OC_DB.Exec(fmt.Sprintf("ALTER TABLE %v ADD INDEX idx_file_link (`dir_link`)", CreateDirOrFileTable(username, "file_")))
}

// 创建根目录
func CreateRoot(username string) {
	global.OC_DB.Table(CreateDirOrFileTable(username, "dir_")).Omit("p_id").Create(&file.Dir{Name: "", ID: 0, Link: ","})
}

// 生成表明
func CreateDirOrFileTable(username string, tableName string) string {
	return fmt.Sprintf("%v%v%v_%v", global.OC_CONFIG.Mysql.TempTable, tableName, global.OC_CONFIG.Mysql.Prefix, username)
}
